package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dao.ISalaryChangesDAO;
import org.jsola.hr.dto.SalaryChangesAddDTO;
import org.jsola.hr.dto.SalaryChangesUpdateDTO;
import org.jsola.hr.entity.SalaryChangesDO;
import org.jsola.hr.query.SalaryChangesQuery;
import org.jsola.hr.service.ISalaryChangesService;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.hr.vo.SalaryChangesVO;
import org.jsola.module.ModuleContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static org.jsola.hr.core.AbstractTest.SITE_ID;
import static org.jsola.hr.core.TestCommonConstans.*;


/**
 * @author zhr
 */
@Component
public class SalaryChangesTestHelper extends BaseTestHelper {
    
    @Autowired
    private ISalaryChangesService salaryChangesService;

    @Autowired
    private ISalaryChangesDAO salaryChangesDAO;

    /**
     * 生成add测试数据
     * @return SalaryChangesAddDTO
     */
    public SalaryChangesAddDTO getSalaryChangesAddDTO(){
        SalaryChangesAddDTO salaryChangesAddDTO = new SalaryChangesAddDTO();
        salaryChangesAddDTO.setEmpId(getRandomValue("varchar",32));
        salaryChangesAddDTO.setEffectiveStatus(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        salaryChangesAddDTO.setEffectiveDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        salaryChangesAddDTO.setChangeReason(getRandomValue("varchar",255));
        salaryChangesAddDTO.setSalaryNew(SafeKit.getInteger(getRandomValue("int",11)));
        salaryChangesAddDTO.setTrafficSubsidyNew(SafeKit.getInteger(getRandomValue("int",11)));
        salaryChangesAddDTO.setMealSubsidyNew(SafeKit.getInteger(getRandomValue("int",11)));
        salaryChangesAddDTO.setSalaryPerformanceNew(SafeKit.getInteger(getRandomValue("int",11)));
        salaryChangesAddDTO.setSalaryTotalNew(SafeKit.getInteger(getRandomValue("int",11)));
        salaryChangesAddDTO.setRemark(getRandomValue("varchar",255));
        salaryChangesAddDTO.setChangeType(SafeKit.getInteger(getRandomValue("tinyint",4)));
        salaryChangesAddDTO.setCompanyId(getRandomValue("varchar",32));
        return salaryChangesAddDTO;
    }


    /**
     * 生成update测试数据
     * @return SalaryChangesUpdateDTO
     */
    public SalaryChangesUpdateDTO getSalaryChangesUpdateDTO(SalaryChangesVO salaryChangesVO){
        return salaryChangesService.selectDOById(salaryChangesVO.getId(), SITE_ID).to(SalaryChangesUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return SalaryChangesQuery
     */
    public SalaryChangesQuery getSalaryChangesQuery() {
        SalaryChangesQuery salaryChangesQuery = new SalaryChangesQuery();
        List<SalaryChangesDO> salaryChangesDOList = salaryChangesService.list(new SalaryChangesDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(salaryChangesDOList)) {
            SalaryChangesDO salaryChangesDO = salaryChangesDOList.get(0);
            salaryChangesQuery = salaryChangesDO.to(SalaryChangesQuery.class);
            if (salaryChangesDO.getEffectiveDate() != null) {
                salaryChangesQuery.setEffectiveDateStart(DateKit.getBeforeDay(salaryChangesDO.getEffectiveDate(),-1));
                salaryChangesQuery.setEffectiveDateEnd(DateKit.getBeforeDay(salaryChangesDO.getEffectiveDate(),1));
            }
            if (salaryChangesDO.getGmtCreate() != null) {
                salaryChangesQuery.setGmtCreateStart(DateKit.getBeforeDay(salaryChangesDO.getGmtCreate(),-1));
                salaryChangesQuery.setGmtCreateEnd(DateKit.getBeforeDay(salaryChangesDO.getGmtCreate(),1));
            }
            if (salaryChangesDO.getGmtModified() != null) {
                salaryChangesQuery.setGmtModifiedStart(DateKit.getBeforeDay(salaryChangesDO.getGmtModified(),-1));
                salaryChangesQuery.setGmtModifiedEnd(DateKit.getBeforeDay(salaryChangesDO.getGmtModified(),1));
            }
        }
        return salaryChangesQuery;
    }

    /**
     * 保存示例
     * @return SalaryChangesVO
     */
    public SalaryChangesVO saveSalaryChangesExample(){
        SalaryChangesAddDTO addDTO = this.getSalaryChangesAddDTO();
        SalaryChangesVO salaryChangesVO = salaryChangesService.save(addDTO,buildTokenUser());
        return salaryChangesVO;
    }

    /**
     * 批量生成add测试数据
     * @return EmpSecretKeysAddDTO
     */
    public ModuleContext batchSaveSalaryChangesExample(ModuleContext moduleContext){
        List<EmpInfoVO> empInfoVOList = moduleContext.get(CODE_EMP_LIST);
        List<SalaryChangesDO> salaryChangesDOList = empInfoVOList.stream().map(empInfoVO -> {
            SalaryChangesAddDTO salaryChangesAddDTO = new SalaryChangesAddDTO();
            salaryChangesAddDTO.setEmpId(empInfoVO.getId());
            salaryChangesAddDTO.setEffectiveStatus(SafeKit.getBoolean(getRandomValue("tinyint",1)));
            salaryChangesAddDTO.setEffectiveDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
            salaryChangesAddDTO.setChangeReason(getRandomValue("varchar",255));
            salaryChangesAddDTO.setSalaryNew(SafeKit.getInteger(getRandomValue("int",11)));
            salaryChangesAddDTO.setRemark(getRandomValue("varchar",255));
            salaryChangesAddDTO.setCompanyId(empInfoVO.getCompanyId());
            return salaryChangesAddDTO.to(SalaryChangesDO.class);
        }).collect(Collectors.toList());

        salaryChangesDAO.insertListAndSetId(salaryChangesDOList);
        moduleContext.add(SALARY_CHANGES_DO_LIST, salaryChangesDOList);
        return moduleContext;
    }


}

